package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketCart;
import com.cskaoyan.bean.zhangsenqi.*;
import com.cskaoyan.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangsenqi
 * @Description
 * @Create 2022-06-29 11:15
 * @Version
 */
@RestController
@RequestMapping("wx/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @RequestMapping("index")
    public BaseRespVo<CartData> index() {
        CartData cartData = cartService.index();
        // 未登录
        if (cartData == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        return BaseRespVo.ok(cartData);
    }

    @PostMapping("checked")
    public BaseRespVo<CartData> checked(@RequestBody CheckedParam checkedParam) {
        CartData cartData = cartService.checked(checkedParam);
        // 未登录
        if (cartData == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        return BaseRespVo.ok(cartData);
    }

    @PostMapping("add")
    public BaseRespVo<Long> add(@RequestBody MarketCart marketCart) {
        long count = cartService.add(marketCart);
        // 未登录
        if (count == -1) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        return BaseRespVo.ok(count);
    }

    @PostMapping("update")
    public BaseRespVo<Long> update(@RequestBody MarketCart marketCart) {
        cartService.update(marketCart);
        return BaseRespVo.ok(null);
    }

    @PostMapping("delete")
    public BaseRespVo<CartData> delete(@RequestBody DeleteParam deleteParam) {
        CartData cartData = cartService.delete(deleteParam.getProductIds());
        // 未登录
        if (cartData == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        return BaseRespVo.ok(cartData);
    }

    @RequestMapping("checkout")
    public BaseRespVo<CheckOutData> checkout(CheckedOutParam checkedOutParam) {
        CheckOutData checkout = cartService.checkout(checkedOutParam);
        // 未登录
        if (checkout == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        return BaseRespVo.ok(checkout);
    }

    @RequestMapping("goodscount")
    public BaseRespVo<Integer> goodscount() {
        Integer goodscount = cartService.goodscount();
        // 未登录
        if (goodscount == -1) {
            return BaseRespVo.ok(0);
        }
        return BaseRespVo.ok(goodscount == null ? 0 : goodscount);
    }

    @PostMapping("fastadd")
    public BaseRespVo<Integer> fastadd(@RequestBody MarketCart marketCart) {
        Integer cartId = cartService.fastadd(marketCart);
        // 未登录
        if (cartId == -1) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setErrmsg("请登录");
            return baseRespVo;
        }
        return BaseRespVo.ok(cartId);
    }
}
